#pragma once
/**
* \file Singleton.h
* \brief Singleton pattern implement.
* \date	2005.07.28 ; 2005.10.24 ; 2005.10.31 \n
* 
* \def SINGLETON_OBJECT(type)
* Add the macro at the beginning of class declaration.
* Any class that wants to be a singleton-class can includes the Macro like Q_OBJECT
*/
#ifdef QT_THREAD_SUPPORT
#include <qmutex.h>


template<class T>
class SingletonHolder
{
public:
	static T* getInstance(){
		if(0 == _instance){
			QMutexLocker locker(getLock());
			if(0 == _instance){
				static T instance;
				_instance = &instance;
			}
		}
		return _instance;
	}

private:
	SingletonHolder<T>(){}
	~SingletonHolder<T>(){}

private:
	static QMutex* getLock(){
		static QMutex lock;
		return &lock;
	}
	static T *  volatile _instance;

};

template<class T>
T * volatile SingletonHolder<T>::_instance=0;

#define SINGLETON_OBJECT(type)	public:\
	static type* getInstance(){	\
	return SingletonHolder<type>::getInstance()	;\
}	\
	private:	\
	friend class SingletonHolder<type>;


#else

#define SINGLETON_OBJECT(type)		public:		\
	static type* getInstance(){	\
	static type* instance=0;					\
	if (instance==0)							\
	instance=new type();						\
	return instance;							\
	}											\
private:					

#endif
